package com.fanjun.learning.io.bio;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by memedai on 2017/2/13.
 */
public class TimeServer {

    public static void main(String[] args) throws IOException {
        int port = 8080;
        if (args != null && args.length > 0){
            try{
                port = Integer.valueOf(args[0]);
            }catch (NumberFormatException ex){
                port = 8080;
            }
        }
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("The timeserver is start with port: " + port);
            Socket socket = null;
            while (true){
                socket = serverSocket.accept();
                new Thread(new TimeServerHandler(socket)).start();
            }
        }finally {
            if (serverSocket != null){
                System.out.println("The time server is close");
                serverSocket.close();
            }
        }
    }

}
